package edu.uwosh.cs342.group2.project4;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import edu.uwosh.cs342.group2.project4.database.ThreadedDbHelper;
import edu.uwosh.cs342.group2.project4.storage.Category;
import edu.uwosh.cs342.group2.project4.storage.Location;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MapOverlay extends com.google.android.maps.Overlay {
	private GeoPoint p;
	private Context c;
	private View popUp;
	private Context places;
	private ThreadedDbHelper db;
	private Location ln;
	private ArrayList<String> favorites;
	private ArrayList<Location> loc = new ArrayList<Location>();

	private boolean isPinch = false;

	public MapOverlay(GeoPoint p, Context c, Context places) {
		this.p = p;
		this.c = c;
		this.places = places;
		

	}

	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		super.draw(canvas, mapView, shadow);

		// ---translate the GeoPoint to screen pixels---
		Point screenPts = new Point();
		mapView.getProjection().toPixels(p, screenPts);
	
		// ---add the marker---
		Bitmap bmp = BitmapFactory.decodeResource(c.getResources(),
				R.drawable.bluedot);
		canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
		
		return true;
	}

	@Override
	public boolean onTap(GeoPoint p, MapView map) {
		if (isPinch) {
			return false;
		} else {

			if (p != null) {

				Places mApp = (Places) places;
				loc = mApp.getArray();

				for (Location l : loc) {
					long lat = p.getLatitudeE6();
					long lng = p.getLongitudeE6();

					String strLat = String.valueOf(lat);
					String strLng = String.valueOf(lng);

					long lat1 = l.getCoordinates().getCords().getLatitudeE6();
					long lng1 = l.getCoordinates().getCords().getLongitudeE6();

					String strLat1 = String.valueOf(lat1);
					String strLng1 = String.valueOf(lng1);
					strLat1 = strLat1.substring(0, 5);
					strLng1 = strLng1.substring(0, 6);
					strLat = strLat.substring(0, 5);
					strLng = strLng.substring(0, 6);
					if (strLat1.equals(strLat) && strLng1.equals(strLng)) {
						handleGeoPoint(l, map);
					}

				}

				return true; // We handled the tap
			} else {
				return false; // Null GeoPoint
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		int fingers = e.getPointerCount();
		if (e.getAction() == MotionEvent.ACTION_DOWN) {
			isPinch = false; // Touch DOWN, don't know if it's a pinch yet
		}
		if (e.getAction() == MotionEvent.ACTION_MOVE && fingers == 2) {
			isPinch = true; // Two fingers, def a pinch
		}
		return super.onTouchEvent(e, mapView);
	}

	public void handleGeoPoint(Location l, MapView map) {
		map.removeView(popUp);
		ln = l;
		LayoutInflater inflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popUp = inflater.inflate(R.layout.balloon, map, false);
		TextView t = (TextView) popUp.findViewById(R.id.bubbleText);
		t.setText(l.getName() + "\n" + l.getDescription());
		MapView.LayoutParams mapParams = new MapView.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, l.getCoordinates()
						.getCords(), 90, 0, MapView.LayoutParams.BOTTOM_CENTER);
		map.addView(popUp, mapParams);
		
		Button balloon = (Button) popUp.findViewById(R.id.bubble);
		
		balloon.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				
				ThreadedDbHelper db = new ThreadedDbHelper(c);
				String cat = ln.getDescription();
				List<Category> cg = db.GetAllCategories();
				String[] newCat = cat.split(" ");
				boolean found = false;
				for(Category ca: cg){
					if(ca.getName().equals(newCat[0])){
						found = true;
					}
				}
				if(!found){
					
					db.AddCategory(newCat[0]);
				}
				cg = db.GetAllCategories();
				int id = 0;
				for(Category ce: cg){
					if(ce.getName().equals(newCat[0])){
						id = ce.getId();
					}
				}
				
				ln.setCategory(new Category(id));
				ln.setLocation("Oshkosh");
				db.AddLocation(ln);
				
				Toast.makeText(c, "Location added to " + newCat[0] , Toast.LENGTH_LONG).show();
			}
			
		});
		
	}

}
